const Service = require("egg").Service;
// const dayjs = require('dayjs');
const idGenerateUtil = require("@jianghujs/jianghu/app/common/idGenerateUtil");
const validateUtil = require("@jianghujs/jianghu/app/common/validateUtil");

const actionDataScheme = Object.freeze({
	importData: {
		type: "array",
		additionalProperties: true,
		items: { type: "object" },
	},
	whereInCondtion: {
		type: "object",
		additionalProperties: true,
		properties: {
			key: { type: "string" },
			values: { type: "array" },
		},
		required: ["key", "values"],
	},
});

// 付款申请时修改对应记录的状态和车辆状态
class StatusService extends Service {
	async _updateCarStatus(trx, tableName) {
		const {
			actionData: { carId },
		} = this.ctx.request.body.appData;

		await trx(tableName).where({ carId }).jhUpdate({ status: "已维修" });
		await trx("basic").where({ carId }).jhUpdate({ operationStatus: "可用", notOperationReason: "" });
	}

	async updateCarAndRepairStatus() {
		const { jianghuKnex } = this.app;
		await jianghuKnex.transaction(async trx => {
			await this._updateCarStatus(trx, "repair");
		});
	}

	async updateCarAndMaintenanceStatus() {
		const { jianghuKnex } = this.app;
		await jianghuKnex.transaction(async trx => {
			await this._updateCarStatus(trx, "maintenance");
		});
	}
	async updateCarStatusAfterHook() {
		console.log("updateCarStatusAfterHook", this.ctx.request.body.appData);
		let { insertAfterUpdate, actionData } = this.ctx.request.body.appData;
		const { knex } = this.app;
		await knex("basic").where({ carId: actionData.carId }).update({
			operationStatus: insertAfterUpdate.status,
			notOperationReason: insertAfterUpdate.reason,
		});
	}
	// 批量导入
	async importData(actionData) {
		let { data } = actionData;
		let { jianghuKnex } = this.app;
		validateUtil.validate(actionDataScheme.importData, data, "数据导入");
		await jianghuKnex.transaction(async trx => {
			const idSequence = await idGenerateUtil.idPlus({
				knex: this.app.jianghuKnex,
				tableName: "basic",
				columnName: "idSequence",
			});
			data = data.map((item, index) => {
				let newIdSequence = idSequence + index;
				let bizIdValue = "";
				let prefix = "CL";
				if (newIdSequence.toString().length < 2) {
					bizIdValue = prefix + "00" + newIdSequence;
				} else if (newIdSequence.toString().length < 3) {
					bizIdValue = prefix + "0" + newIdSequence;
				} else {
					bizIdValue = prefix + newIdSequence;
				}
				return {
					idSequence: newIdSequence,
					carId: bizIdValue,
					...item,
				};
			});
			trx("basic").jhInsert(data);
			// for (let i = 0; i < data.length; i++) {
			// 	trx("basic").jhInsert(data[i]);
			// }
		});
	}
	// 批量删除
	async bacthDelete(actionData) {
		let { knex } = this.app;
		let { carIds } = actionData;
		await knex.transaction(async trx => {
			await this._updateCloumnStatus(trx, { key: "carId", values: carIds }, { isDelete: 1 });
		});
	}
	// 批量报废
	async batchScrap(actionData) {
		let { knex } = this.app;
		let { carIds } = actionData;
		await knex.transaction(async trx => {
			await this._updateCloumnStatus(
				trx,
				{ key: "carId", values: carIds },
				{ operationStatus: "不可用", notOperationReason: "已报废" }
			);
		});
	}
	// 更新某一个字段的状态
	async _updateCloumnStatus(trx, whereInCondtion, data) {
		validateUtil.validate(actionDataScheme.whereInCondtion, whereInCondtion, "whereIn数据验证");
		await trx("basic").whereIn(whereInCondtion.key, whereInCondtion.values).update(data);
	}
}
module.exports = StatusService;
